// dao/userDao.js
// ʵ����MySQL����
var mysql = require('mysql');
var crypto= require('crypto')
var $conf = require('../../conf/db');
var $util = require('../utils/util');
var $sql = require('../mapping/userSqlMapping');

var pool = mysql.createPool($util.extend({}, $conf.mysql));

module.exports = {
    queryPassword: function (req, res, callback) {
        pool.getConnection(function (err, connection) {
            var hasher=crypto.createHash("md5");
            var key = hasher.update(req.body.param).digest('hex');
            connection.query($sql.queryPassword, [req.session.currentUser.id,key], function (err, result) {
                if (result.length >= 1) {
                    result = {
                        code: 200,
                        content: result
                    };
                    callback(result);
                } else {
                    result = {
                        code: 0,
                        content: result
                    };
                    callback(result);
                }
                // �ͷ�����
                connection.release();
            });
        });
    },



    login: function (req, res, callback) {
        //var id = +req.query.id; // ???????????sql???????????????
        pool.getConnection(function (err, connection) {
            var hasher=crypto.createHash("md5");
            var key = hasher.update(req.param("password")).digest('hex');
            connection.query($sql.login, [req.param("username"),key], function (err, result) {
                callback(result);
                connection.release();
            })
        })
    },
    checkUsername: function (req, res, callback) {
        pool.getConnection(function (err, connection) {
            connection.query($sql.checkUsername, req.param("username"), function (err, result) {
                callback(result);
                connection.release();
            });
        });
    },
    register: function (req, res, userInfoId, callback) {
        console.log(userInfoId);
        var date = $util.getDate();
        pool.getConnection(function (err, connection) {
            var hasher=crypto.createHash("md5");
            var key = hasher.update(req.param("password")).digest('hex');
            connection.query($sql.add, [req.param("username"),key, userInfoId, date, 3], function (err, result) {
                callback(result);
                connection.release();
            });
        });
    },

    updatePassword: function (req, res, callback) {//修改用户密码
        pool.getConnection(function (err, connection) {
            var hasher=crypto.createHash("md5");
            var key = hasher.update(req.body.password1).digest('hex');
        //    var password1 = req.body.password1;
            connection.query($sql.updatePassword, [key,req.session.currentUser.id], function (err, result) {
                if (result.affectedRows >= 1) {
                    result = {
                        code: 200,
                        content: result
                    };
                    callback(result);

                } else {
                    result = {
                        code: 0,
                        content: result
                    };
                    callback(result);
                }
                connection.release();
            });
        })
    },

    addUser: function (req, res, userInfoId, callback) { //管理员添加用户 默认密码123456

        console.log(userInfoId);
        var date = $util.getDate();
        var hasher=crypto.createHash("md5");
        var key = hasher.update("123456").digest('hex');

        pool.getConnection(function (err, connection) {
            //管理员添加用户
            connection.query($sql.add, [req.body.tel, key, userInfoId, date, 3], function (err, result) {
                if (result.affectedRows >= 1) {
                    result = {
                        code: 200,
                        content: result
                    };
                    callback(result);

                } else {
                    callback("no");
                }
                //释放连接
                connection.release();
            });
        });
    },
    delUser: function (req, res, callback) { //管理员删除用户

        //console.log(userInfoId);
       // var date = this.getDate();
        pool.getConnection(function (err, connection) {
            //管理员添加用户
            connection.query($sql.del, [req], function (err, result) {
                if (result.affectedRows >= 1) {
                    result = {
                        code: 200,
                        content: result
                    };
                    callback(result);

                } else {
                    callback("no");
                }
                //释放连接
                connection.release();
            });
        });
    }

}
